package com.travelmood.controller;

import com.travelmood.pojo.Result;
import com.travelmood.pojo.Diary;
import com.travelmood.pojo.User;
import com.travelmood.service.DiaryService;
import com.travelmood.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/diarys")
public class DiaryController {

    @Autowired
    private UserService userService;
    @Autowired
    private DiaryService diaryService;
    //1.token ------> uid
    //2.设置diary.uid = uid
    //3.将 diary 作为参数传入 mapper 接口和 xml 文件中
    @PostMapping("/add")
    public Result addDiary(@RequestHeader("Authorization") String token, @RequestBody Diary diary,@RequestParam Integer placeId) {
        log.info("Received diary: {}", diary);
        try {
            User user = userService.getUserInfoByToken(token);
            diaryService.addDiary(diary,user.getUserId(),placeId);
            return Result.success();
        } catch (Exception e) {
            log.error("Error adding diary", e);
            return Result.error("发生错误，无法添加手账");
        }
    }
    //1.根据 token 解析出来 uid，
    //2.将 uid 和事项信息存储数据库

    //1.getHead 得到 token
    //2.jwt 解析 token 得到 claims
    //3.claims 得到 uid
    ////查看用户所有手账
    @GetMapping("/mydiarys")
    public Result getMydiarys(@RequestHeader("Authorization") String token){
        //⭐⭐
        try {
            User user = userService.getUserInfoByToken(token);
            List<Diary> diarys = diaryService.getDiarysByUserId(user.getUserId());
            return Result.success(diarys);
        } catch (Exception e) {
            return Result.error("错误");
        }
    }
    //查看指定手账
    @GetMapping("/find")
    public Result findDiary(@RequestHeader("Authorization") String token, @RequestParam Integer diaryId) {
        try {
            User user = userService.getUserInfoByToken(token);
            Diary diary = diaryService.findDiaryById(diaryId);
            if (diary == null) {
                return Result.error("未找到手账");
            }
            return Result.success(diary);
        } catch (Exception e) {
            return Result.error("发生错误，无法查询手账");
        }
    }

    @GetMapping("/getPlaceId")
    public Result getPlaceId(@RequestHeader("Authorization") String token, @RequestParam Integer diaryId) {
        try {
            User user = userService.getUserInfoByToken(token);
            Integer placeId = diaryService.getPlaceIdById(diaryId);
            if (placeId == null) {
                return Result.error("未找到手账");
            }
            return Result.success(placeId);
        } catch (Exception e) {
            return Result.error("发生错误，无法查询手账");
        }
    }

    @PutMapping("/update")
    public Result update(@RequestHeader("Authorization") String token, @RequestBody Diary diary){
        try {
            User user = userService.getUserInfoByToken(token);
            diaryService.updatediary(diary, user.getUserId());
            return Result.success();
        } catch (Exception e) {
            return Result.error("发生错误，无法更新手账");
        }
    }

    @DeleteMapping("/delete")
    public Result deleteDiary(@RequestHeader("Authorization") String token, @RequestParam Integer diaryId) {
        try {
            User user = userService.getUserInfoByToken(token);
            diaryService.deleteDiaryPlaceById(diaryId);
            diaryService.deleteDiaryById(diaryId, user.getUserId());
            return Result.success();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("发生错误，无法删除手账");
        }
    }

}












